/*******************************************************************************************
 * Copyright (c) 2006-2009 Alex Robson, Rob Simmons, Craig Israel, and Rachel Twyford
 *
 * Use of this software in any form requires that you have both 
 * read and agreed to the following terms:
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the "Software"), 
 * to deal in the Software without restriction, including without limitation 
 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions: 
 *
 * Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright notice, 
 * this list of conditions and the following disclaimer in the 
 * documentation and/or other materials provided with the distribution.
 * Neither the name of Nvigorate nor the names of its contributors may 
 * be used to endorse or promote products derived from this software without 
 * specific prior written permission. 
 *
 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
 * DISCLAIMED. IN NO EVENT SHALL ANY CONTRIBUTOR BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * ****************************************************************************************/
 
using System.Data;
using System.Data.Odbc;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Text.RegularExpressions;
//using MySql.Data.MySqlClient;
//using Oracle.DataAccess.Client;

namespace Nvigorate.Data
{
    public partial class DataInterface : IDataInterface
    {
        #region Connection String Templates
        private static  string GenericODBC =
            "DSN=<mydsn>;Uid=<username>;Pwd=<passwd>";

        //MySQL Database connection strings
        private static  string MySQL_OLEDB =
            "Provider=MySQLProv;Data Source= <datasource>;User Id=<username>;Password=<passwd>;";
        private static  string MySQL_ODBC_25Local =
            "Driver={mySQL};Server=localhost;Option=16834;Database=<database>;";
        private static  string MySQL_ODBC_25Remote =
            "Driver={mySQL};Server=<sqlserver>;Port=<port>;Option=131072;Stmt=<statement>;Database=<database>;uid=<username>;Pwd=<passwd>;";
        private static  string MySQL_ODBC_351Local =
            "DRIVER={MYSQL ODBC 3.51 Driver};SERVER=localhost;DATABASE=<database>;USER=<username>;PASSWORD=<passwd>;OPTION=3;";
        private static  string MySQL_ODBC_351Remote =
            "DRIVER={MYSQL ODBC 3.51 Driver};SERVER=<sqlserver>;PORT=<port>;DATABASE=<database>;USER=<username>;PASSWORD=<passwd>;OPTION=3;";
		
		


        //Oracle Database connection strings
        private static  string Oracle_ODBC_New =
            "Driver={Microsoft ODBC for Oracle};Server=<oracleserver>;Uid=<username>;Pwd=<passwd>;";
        private static  string Oracle_ODBC_Old =
            "Driver={Microsoft ODBC Driver for Oracle};ConnectString=<oracleserver>;Uid=<username>;Pwd=<passwd>;";
        private static  string Oracle_OLEDB_MS =
            "Provider=msdaora;Data Source=<oracledb>;User Id=<username>;Password=<passwd>;";
        private static  string Oracle_OLEDB_OR =
            "Provider=OraOLEDB.Oracle;Data Source=<oracledb>;User Id=<username>;Password=<passwd>;";
        private static  string Oracel_NET =
            "Data Source=<oracledb>;User Id=<username>;Password=<passwd>;Integrated Security=no";


        //MS Access Database connection strings
        private static  string MSAccess_ODBC_Standard =
            "Driver={Microsoft Access Driver (*.mdb)};Dbq=<accessdb>;Uid=<username>;Pwd=<passwd>;";
        private static  string MSAccess_ODBC_Workgroup =
            "Driver={Microsoft Access Driver (*.mdb)};Dbq=<accessdb>;SystemDB=<mdw>;Uid=<username>;Pwd=<passwd>;";
        private static  string MSAccess_ODBC_Exclusive =
            "Driver={Microsoft Access Driver (*.mdb)};Dbq=<accessdb>;Exclusive=1;Uid=<username>;Pwd=<passwd>;";
        private static  string MSAccess_OLEDB_Standard =
            "Provider=Microsofft.Jet.OLEDB.4.0;Data Source=<accessdb>;User Id=<username>;Password=<passwd>;";
        private static  string MSAccess_OLEDB_Workgroup =
            "Provider=Microsofft.Jet.OLEDB.4.0;Data Source=<accessdb>;Jet OLEDB:System Database=<mdw>;";
        private static  string MSAccess_OLEDB_Password =
            "Provider=Microsofft.Jet.OLEDB.4.0;Data Source=<accessdb>;Jet OLEDB:Password=<passwd>;";


        //IBM DB2 Database connection strings
        private static  string IBMDB2_OLEDB =
            "Provider=DB2OLEDB;Network Transport Library=TCPIP;Network Address=<ip>;Initial Catalog=<catalog>;Package Collection=<collection>;Default Schema=<schema>;User ID=<username>;Password=<passwd>;";
        private static  string IBMDB2_ODBC =
            "Driver={IBM DB2 ODBC DRIVER};Database=<database>;hostname=<host>;port=<port>;protocol=TCPIP;uid=<username>;pwd=<passwd>;";


        //Microsoft SQL Database connection strings
        private static  string MSSQL_NET_Standard =
            "Server=<server>;Database=<catalog>;User ID=<username>;Password=<passwd>;Trusted_Connection=False;";
        private static  string MSSQL_NET_Trusted =
            "Server=<server>;Database=<catalog>;Trusted_Connection=True;";
        private static  string MSSQL_NET_TCPIP_PORT =
            "Data Source=<ip>,<port>;Network Library=DBMSSOCN;Initial Catalog=<catalog>;User ID=<username>;Password=<passwd>;";
        private static string MSSQL_NET_TCPIP =
            "Data Source=<ip>;Network Library=DBMSSOCN;Initial Catalog=<catalog>;User ID=<username>;Password=<passwd>;";

        #endregion
        
        #region Private Static Methods
        /// <summary>
        /// Iterates through tag/value pairs in the source string and replaces the tags with
        /// the values to create a valid connection string.
        /// </summary>
        /// <param name="source">The source string with imbedded tags</param>
        /// <param name="pairs">The tag name / tag value pairs</param>
        /// <returns>The completed connection string</returns>
        private	static	string	ReplaceTags(string source, params string[] pairs)
        {
            Regex   rgx;
            string	connectionString	= source;
            
            foreach (string pair in pairs)
            {
                rgx = new Regex(pair.Split(';')[0]);
                connectionString = rgx.Replace(connectionString, pair.Split(';')[1]);
            }
            return connectionString;
        }
        #endregion
    }
}
